
<template>
  <el-dialog v-model="dialogFormVisible" :title="title" width="1200">
    <el-form :model="form" ref="formRef" label-width="120" style="max-width: 1200px" :rules="rules">
      <div class="form-scroll">
        <el-row :gutter="20">

          <el-col :span="24">
            <el-form-item label="封面" :prop="modelUrl">
              <imageUpload v-model="form.modelUrl"></imageUpload>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="模型代码" prop="modelCode">
              <el-input v-model="form.modelCode" placeholder="请填写模型代码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" placeholder="请填写名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="模型类型" prop="modelType">
                <el-select v-model="form.modelType" placeholder="请选择1脸型  2体型" clearable>
                  <el-option label="脸型" :value="1"></el-option>
                  <el-option label="体型" :value="2"></el-option>
                </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="体型" prop="bodyType">
              <el-select v-model="form.bodyType" placeholder="请选择体型">
                <el-option
                    v-for="dict in jx3_body"
                    :key="dict.value"
                    :label="dict.label"
                    :value="Number(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="脸型类型" prop="faceType">
              <el-select v-model="form.faceType" placeholder="请选择1脸型  2体型" clearable>
                <el-option label="写实" :value="1"></el-option>
                <el-option label="写意" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="模型描述" prop="modelDesc">
              <el-input v-model="form.modelDesc" placeholder="请填写模型描述" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="来源" prop="source">
              <el-input v-model="form.source" placeholder="请填写来源" />
            </el-form-item>
          </el-col>
        </el-row>
      </div>
    </el-form>
    <div>
      <el-button type="primary" @click="onSubmit">{{ isEdit? '编辑': '新增' }}</el-button>
      <el-button @click="dialogFormVisible = false">取消</el-button>
    </div>
  </el-dialog>
</template>

<script setup>
    // 方法
    import { ref, defineExpose, defineEmits } from 'vue'
    const instance = getCurrentInstance();
    const { proxy } = instance || {};
    const { jx3_body } = proxy.useDict("jx3_body")
    import methods from '@/utils/methods.js'

    // 接口
    import { addModel, getModel, updateModel } from '@/api/jx3/model.js'

    // 组件
    import imageUpload from '@/components/ImageUpload/index.vue'

    // 显示表单
    const dialogFormVisible = ref(false)
    // 弹窗标题
    const title = ref('新增模型')
    // 是否为编辑
    const isEdit = ref(false)
    // 表单节点
    const formRef = ref(null)
    // 表单
    const form = ref({
      id: undefined, //
      modelUrl: undefined, // 封面
      modelCode: undefined, // 模型代码
      name: undefined, // 名称
      modelType: undefined, // 1脸型  2体型
      bodyType: undefined, // 体型  1成男 2正太 3成女 4萝莉
      faceType: undefined, // 脸型类型 1写实 2写意
      modelDesc: undefined, // 模型描述
      auditOpinion: undefined, // 审核意见
      source: undefined, // 来源
      auditStatus: undefined, // 1待审核 2审核中
      userId: undefined, // 用户id
      createBy: undefined, // 创建人
      createTime: undefined, // 创建时间
      updateTime: undefined, // 更新时间
      updateBy: undefined, // 更新人
    })
    // 验证
    const rules = ref({
      // 封面
      modelUrl: [
        { required: true, message: '请上传封面', trigger: 'blur' }
      ],
      // 模型代码
      modelCode: [
        { required: true, message: '请填写模型代码', trigger: 'blur' }
      ],
      // 名称
      name: [
        { required: true, message: '请填写名称', trigger: 'blur' }
      ],
      // 1脸型  2体型
      modelType: [
        { required: true, message: '请选择1脸型  2体型', trigger: 'blur' }
      ],
      // 体型  1成男 2正太 3成女 4萝莉
      bodyType: [
        { required: true, message: '请选择体型  1成男 2正太 3成女 4萝莉', trigger: 'blur' }
      ],
      // 脸型类型 1写实 2写意
      faceType: [
        { required: true, message: '请选择脸型类型 1写实 2写意', trigger: 'blur' }
      ],
    })

    const emits = defineEmits(['change'])

    // 打开方法
    const open = () => {
      for (let i in form.value) {
        form.value[i] = ''
      }
      dialogFormVisible.value = true
      isEdit.value = false
      title.value = '新增模型'
    }
    // 编辑
    const edit = (data) => {
      getModel(data.id).then(res => {
        for (let i in res.data) {
          form.value[i] = data[i]
        }
      })
      isEdit.value = true
      dialogFormVisible.value = true
      title.value = '编辑模型'
    }
    // 关闭方法
    const close = () => {
      dialogFormVisible.value = false
    }

    // 提交
    const onSubmit = async () => {
      await formRef.value.validate((valid, fields) => {
        if (valid) {
          const params = methods.deepClone(form.value)
          if(params.id) {
            updateModel(params).then(res => {
              if(res.code === 200) {
                proxy.$modal.msgSuccess(title.value + '成功');
                close()
                emits('change')
              }
            })
          }else {
            addModel(params).then(res => {
              if(res.code === 200) {
                proxy.$modal.msgSuccess(title.value + '成功');
                close()
                emits('change')
              }
            })
          }
        }
      })
    }

    defineExpose({
      open, close, edit
    })
</script>

<style scoped lang="scss">

</style>
